<?php

namespace app\common\model\notice;

use app\common\enum\notice\EmailEnum;
use app\common\enum\YesNoEnum;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 邮件记录模型
 * Class EmailLog
 * @package app\common\model
 */
class EmailLog extends BaseModel
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    /**
     * @notes 验证注册验证码
     * @param array $params
     * @return bool
     * @author 段誉
     * @date 2022/9/7 15:37
     */
    public static function checkCode($email, $code, $scene_id)
    {
        $where = [
            ['email', '=', $email],
            ['scene_id', '=', $scene_id],
            ['send_status', '=', EmailEnum::SEND_SUCCESS],
        ];

        $email_log = self::where($where)
            ->order('send_time', 'desc')
            ->findOrEmpty();

        // 没有验证码 或 最新验证码已校验 或 已过期(有效期：5分钟)
        if($email_log->isEmpty()) {
            return false;
        }

        // 更新校验状态
        if($email_log->code == $code) {
            $email_log->is_verify = YesNoEnum::YES;
            $email_log->save();
            return true;
        }

        return false;
    }
}